<?php

namespace app\apiadmin\service\unionapp;

use app\apiadmin\model\unionapp\UnionAppModel;
use app\apiadmin\service\data\CommStaticDictService;
use app\common\utils\Res;

use think\cache\driver\Redis;    //需要使用到的类
use think\facade\Cache;
use think\facade\Config;
use think\facade\Db;


use app\apiadmin\service\statics\LiveUserGetDataService;
use app\apiadmin\controller\liveManager\LiveStatus;


class UnionAppService
{

    /**
     * @author: zwx
     * @Description: 获取商品信息
     * @Date: 2024-05-16 10:17:55
     */    
    public static function getGoodsInfo($param){

        $map = [];
        $map["o.del"] = 0;
        $map["o.id"] = $param["courseid"];
        $map["o.state"] = 1;

        $findName = "o.*,o.name as coursename,o.img,o.detail_img,t.name as teacher_name,t.img as teacher_img,t.desc as teacher_detail";
        
        $result = UnionAppModel::getGoodsInfo($map,$findName);

        $result['xianjia'] = '¥'.$result['fee']/100;
        $result['yuanjia'] = '¥'.$result['sale']/100;
        $result['txt'] = '去抢购';
       

        //统计打开商品数据
        $liveId = $param['liveId'];  // 直播ID
        $liveStatus = new LiveStatus($liveId);

        $liveStatus->set_dianji_shangpin_cishu($param["courseid"].'_'.$param["user_id"]);
        $liveStatus->set_dianji_shangpin_renshu($param["courseid"].'_'.$param["user_id"]);


        //行为记录添加数据详情
        $collection = 'live_event';
        $liveOrder = new LiveUserGetDataService($collection);
        $map = [];
        $map['del'] = 0;
        $map['user_id'] = $param["user_id"].'';
        $map['type'] = $param["event"];
        $map['event'] = '无';
        $map['courseid'] = $param["courseid"];
        $map['coursename'] = $result['coursename'];
        $map['liveId'] = $param["liveId"];
        $map['create_time'] = time();
        $r = $liveOrder->addEvent($map);
        // 获取插入的文档的_id
        $insertedId = $r->getInsertedId();
        $result['event_id'] =(string)$insertedId;
        $result['$r'] =$r;
        $data = [
            'list' => $result 
        ];

        return $data;

    }

    public static function getShangpinInfo($param){

        $course = db::table('xh_siyu_course')->where(['id'=>$param['courseid']])->find();
        //扣除定金
        // $course['deposit_ids']=[];
        // if(isset($param['user_id']) && isset($param['courseid'])){
        //     $where=[
        //         'siyu_course_id'=>$param['courseid'],
        //         'user_id'=>$param['user_id'],
        //         'state'=>1,
        //         'siyu_order_id'=>0
        //     ];
        //     $deposit_list = Db::name('siyu_deposit')->where($where)->select()->toArray();
        //     if($deposit_list && $course){
        //         foreach($deposit_list as $item){
        //             $course['fee']-=$item['actual_payment_fee'];
        //             $course['deposit_ids'][]=$item['id'];
        //         }
        //     }
        // }
        if($course){
            $course['title'] = $course['name'];
            $course['xianjia'] = '¥'.$course['fee']/100;
            $course['yuanjia'] = '¥'.$course['sale']/100;
            $course['txt'] = '去抢购';
        }
        return $course;
    }

    /**
     * @author: zwx
     * @Description: 创建订单
     * @param {*} $param
     * @Date: 2024-05-16 10:18:33
     */    
    public static function createOrder($param){

        $user = Db::name('siyu_user')->where(['id'=>$param['userid']])->find();
        if(!$user){
            return Res::error('用户不存在');
        }

        $course = Db::name('siyu_course')->where(['id'=>$param['courseid']])->find();
        if(!$course){
            return Res::error('课程不存在');
        }

        $time = time();
        $order_num = 'SY'.$param['order_from'].rand(1000,9999).date('YmdHis',$time);

        $data = array();
        $data['create_time'] = date('Y-m-d H:i:s',time());
        $data['order_num'] = $order_num;
        $data['tel'] = $user['phone'];
        $data['fee'] = $course['fee'];
        $data['order_from'] = $param['order_from'];
        $data['courseid'] = $param['courseid'];
        $data['tutorId'] = $param['tutorId'];
        $result = UnionAppModel::createOrder($data);

        return $result;
    }

    /**
     * @author: zwx
     * @Description: 获取弹卡信息
     * @Date: 2024-05-17 15:49:57
     */    
    public static function getTanKaInfo($param){

        $findName = 't.courseid,c.name as coursename,c.img,c.id,t.btn_txt as txt';

        $data = array();
        $data['t.courseid'] = $param['courseid'];
        $data['t.show'] = 1;
        $data['t.del'] = 0;
        $result = UnionAppModel::getTanKaInfo($data,$findName);

        if(!$result){
            return Res::error('暂无弹卡信息');
        }

        return $result;
    }

    public function dianji_shangpin_renshu($map){

        $liviId = $map['liviId'];
        $courseid = $map['courseid'];
        $openid = $map['openid'];

    }

    public static  function entertime($param){

        // $collection = 'live_user_'.$param['liveId'];
        // $liveuser = new LiveUserGetDataService($collection);  
        // $result = $liveuser->entertime($param);
        // return Res::ok($result);
        return 0;
    }

    public static  function exittime($param){
        // $collection = 'live_user_'.$param['liveId'];
        // $liveuser = new LiveUserGetDataService($collection);
        // $result =  $liveuser->exittime($param);
        // return Res::ok($result);
        return 0;
    }

    public static function getUpdatePrice($param){
        $key=$param['key'];
        $redis = \app\apiadmin\service\data\RedisService::makeService();
        $value = $redis->get($key);
        $result=[];
        if($value){
            $result = json_decode($value,true);
        }
        return $result;
    }
}